package com.newlink.building.login.ui.fragments

import android.content.Context
import android.content.Intent
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.util.Log
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.core.content.ContextCompat
import com.newlink.building.login.R
import com.newlink.building.login.common.Module_Login_Constant
import com.newlink.building.login.databinding.LayoutRegisterBinding
import com.newlink.building.login.ui.Module_Login_RegisterActivityNL
import com.newlink.building.common_base.activities.NL_LicenseActivityNL
import com.newlink.building.common_base.base.BaseFragment

/**
 * @Author: admin
 * @Date: 2022/9/13
 * @Description: 注册账号输入界面
 * @License:  Copyright  1998 Fanvil Technology Co., Ltd. All rights reserved.
 */
class Module_Login_RegisterAccountFragment : BaseFragment() {

    companion object {
        fun getInstance(): Module_Login_RegisterAccountFragment {
            return Module_Login_RegisterAccountFragment()
        }
    }

//    override fun attachLayoutRes(): Int = R.layout.layout_register

    private lateinit var mBinding: LayoutRegisterBinding
    override fun attachLayoutRes(): View {
        mBinding = LayoutRegisterBinding.inflate(layoutInflater)
        return mBinding.root
    }

    override fun initView(view: View) {
        mBinding.registerAccountEt.setBackgroundResource(R.drawable.et_underline_selector)
        mBinding.btnGetVerifyCode.setOnClickListener {
            Log.e("love", "onclick , btn")
            if (!mBinding.cbRegistAgreeLicense.isChecked) {
                mBinding.cbRegistAgreeLicense.error = "请同意方位用户协议"
                return@setOnClickListener
            }

            val content = mBinding.registerAccountEt.text

            if (content?.isEmpty() == true) {
                mBinding.registerAccountEt.error = "账号不能为空"
                return@setOnClickListener
            }
            val phoneMatcher = "[1][358]\\d{9}"
            val emailMatcher = "^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$"

//            val regxPhone = "^[1][3|5|7|8][0-9]{9}$";
            val regxEmail = "^[0-9_A-Za-z]+(\\.[0-9_A-Za-z]+)*@[0-9_A-Za-z]+(\\.[0-9A_Za-z]+)+$";

            if (content!!.matches(Regex(phoneMatcher)) || content.matches(Regex(regxEmail))) {
                activity?.let {

                    // jake todo : 向手机号或者邮箱发送验证码.
                    (activity as Module_Login_RegisterActivityNL).showFragment(Module_Login_Constant.Type.REGISTER_VERIFY);
                }
            } else {
                mBinding.registerAccountEt.error = "输入内容的格式不对"
                return@setOnClickListener
            }

        }

//        setSpannableStringView1(
//            requireContext(),
//            register_license_content,
//            getString(R.string.user_register_license_user),
//            getString(R.string.user_register_license_privacy),
//            R.string.user_register_license
//        )

//        setSpannableStringView(
//            requireContext(),
//            license_content,
//            getString(R.string.user_register_license_content),
//            R.string.user_register_license
//        )

    }

    override fun lazyLoad() {
    }

    private fun setSpannableStringView(
        context: Context,
        tv: TextView,
        content1: String,
        resId: Int
    ) {
        var str = context.getString(resId)
        val index1 = str.indexOf("#")
        str = str.replace("####", content1)
        val spannableString = SpannableStringBuilder(str)
        val linkedForground1 =
            ForegroundColorSpan(ContextCompat.getColor(context, R.color.light_blue))

        with(spannableString) {
            setSpan(
                object : ClickableSpan() {
                    override fun onClick(widget: View) {
                        showUserLicense(context, 0)
                    }

                },
                index1,
                index1 + content1.length,
                Spanned.SPAN_INCLUSIVE_EXCLUSIVE
            )
            setSpan(
                linkedForground1,
                index1,
                index1 + content1.length,
                Spanned.SPAN_INCLUSIVE_EXCLUSIVE
            )
        }
        tv.text = spannableString
        tv.movementMethod = LinkMovementMethod.getInstance()
    }

    private fun setSpannableStringView1(
        context: Context,
        tv: TextView,
        content1: String,
        content2: String,
        resId: Int
    ) {
        var str = context.getString(resId)
        val index1 = str.indexOf("#")
        val index2 = str.indexOf("*") + (content1.length - 4)
        str = str.replace("####", content1)
        str = str.replace("****", content2)
        val spannableString = SpannableStringBuilder(str)
        val linkedForground1 =
            ForegroundColorSpan(ContextCompat.getColor(context, R.color.light_blue))
        val linkedForground2 =
            ForegroundColorSpan(ContextCompat.getColor(context, R.color.light_blue))

        with(spannableString) {
            setSpan(
                object : ClickableSpan() {
                    override fun onClick(widget: View) {
                        Toast.makeText(widget.context, "USER LICENSE", Toast.LENGTH_SHORT).show()
//                        (activity as RegisterActivity).startBrowser(com.vdroid.dooraccess.common_base.constant.Constant.USER_LICENSE_URL)
                        showUserLicense(context, 0)
                    }

                },
                index1,
                index1 + content1.length,
                Spanned.SPAN_INCLUSIVE_EXCLUSIVE
            )
            setSpan(
                linkedForground1,
                index1,
                index1 + content1.length,
                Spanned.SPAN_INCLUSIVE_EXCLUSIVE
            )
            setSpan(
                object : ClickableSpan() {
                    override fun onClick(widget: View) {
                        Toast.makeText(widget.context, "PRIVACY LICENSE", Toast.LENGTH_SHORT).show()
//                        (activity as RegisterActivity).startBrowser(com.vdroid.dooraccess.common_base.constant.Constant.PRIVACY_LICENSE_URL)
                        showUserLicense(context, 1)
                    }

                },
                index2,
                index2 + content2.length,
                Spanned.SPAN_INCLUSIVE_EXCLUSIVE
            )
            setSpan(
                linkedForground2,
                index2,
                index2 + content2.length,
                Spanned.SPAN_INCLUSIVE_EXCLUSIVE
            )
        }

        tv.text = spannableString
        tv.movementMethod = LinkMovementMethod.getInstance()
    }

    fun showUserLicense(context: Context, type: Int) {
        val intent = Intent(context, NL_LicenseActivityNL::class.java).apply {
            action = type.toString()
        }

        context.startActivity(intent)
    }
}